package tech.aistar.corejava.day11.homework.ShoppingCar;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/7/29
 * @Version: 1.0
 */
public class CarDaoImlp implements ICarDao{
    List<Product> list = new ArrayList<>();


    public CarDaoImlp(){

    }
    @Override
    public List<Product> findAll() {
        return list;
    }
    @Override
    public void insert(Product p) {
        list.add(p);
    }

    @Override
    public void delete(Integer id) {
        if(list.isEmpty()){
            System.out.println("没了别删了");
        }
        Iterator<Product> iterator = list.iterator();
        while(iterator.hasNext()){
            Product p = iterator.next();
            if(p.getId() == id){ // 当id相等时采用迭代器的remove方法删除
                iterator.remove();
            }
        }
    }

    @Override
    public void update(String name, String newname, Integer newid, Double newprice) {
        if(list.isEmpty()){
            return;
        }
        Iterator<Product> iterator = list.iterator();
        while(iterator.hasNext()){
            Product p = iterator.next();
            if (p.getName().equals(name)){
                p.setId(newid);
                p.setName(newname);
                p.setPrice(newprice);
            }
        }

    }

    @Override
    public void select(Integer id) {
        if(list.isEmpty()){
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId() == id){
                System.out.println(list.get(i));
            }
        }
    }
}
